import DictTemplate from '@/utils/dict/DictTemplate'
import store from '@/store'
import { mergeOptions } from '@/utils/dict/DictOptions'
import { listByTemplateId, treeByTemplate } from '@/api/sys/dict/data'
import dictionary from "@/utils/dictionary";
function queryDictByKey(dict, key) {
  if (key == null && key == '') {
    return null
  }
  try {
    for (let i = 0; i < dict.length; i++) {
      if (dict[i].key == key) {
        return dict[i].value
      }
    }
  } catch (e) {
    return null
  }
}

export default {
  //必需定义一个install方法
  install(Vue, options) {
    mergeOptions(options)
    //定义一个混入对象
    Vue.mixin({
      data() {
        if (
          this.$options === undefined ||
          this.$options.dictTemplates === undefined ||
          this.$options.dictTemplates === null
        ) {
          return {}
        }
        const dictTemplate = new DictTemplate()
        return {
          dictTemplate,
          dicts: {},
        }
      },
      created() {
        if (
          !(this.dictTemplate instanceof DictTemplate) ||
          this.$options.dictTemplates.length <= 0
        ) {
          return
        }
        this.dictTemplate.init(this.$options.dictTemplates).then(res => {
          res.forEach(async dictTemplate => {
            const storeDict = queryDictByKey(
              store.getters.dictTemplate,
              dictionary[dictTemplate].dictDataField
            )
            if (storeDict && storeDict.length > 0) {
              this.dicts[dictTemplate] = storeDict
            } else {
              //判断是否是级联 （true就是是）
              if (!dictionary[dictTemplate].isCascader) {
                  await listByTemplateId({templateId: dictionary[dictTemplate].templateId})
                    .then(res => {
                      store.dispatch('dictTemplate/setDictTemplate', {
                        key: dictTemplate,
                        value: res.data
                      })
                      this.dicts[dictTemplate] = res.data
                    })
                    .catch(error => {
                      this.$customModal.msgError(error)
                    })
              } else {
                if(dictionary[dictTemplate].isChooseChild){
                  // 获取指定节点
                  await treeByTemplate({templateId: dictionary[dictTemplate].templateId,pcode:dictionary[dictTemplate].isChooseChild})
                    .then(res => {
                      store.dispatch('dictTemplate/setDictTemplate', {
                        key: dictTemplate,
                        value: res.data
                      })
                      this.dicts[dictTemplate] = res.data
                    })
                    .catch(error => {
                      this.$customModal.msgError(error)
                    })
                }else{
                  await treeByTemplate({templateId: dictionary[dictTemplate].templateId})
                    .then(res => {
                      store.dispatch('dictTemplate/setDictTemplate', {
                        key: dictTemplate,
                        value: res.data
                      })
                      this.dicts[dictTemplate] = res.data
                    })
                    .catch(error => {
                      this.$customModal.msgError(error)
                    })
                }
              }
            }
            this.$nextTick(() => {
              this.$forceUpdate()
            })
          })
        })
      }
    })
  }
}
